import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as $ from 'jquery';
import config from '../../common/utils/baseUrl';
import getData_ajax from '../../common/utils/getData_ajax';
import { NzModalService } from 'ng-zorro-antd';
import {Routes, Router} from '@angular/router';
import cookie from '../../common/utils/cookie';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  public user_login_info:any = {
    username:'',
    password:''
  }; // 用户输入信息

  constructor(private http: HttpClient,private modalService: NzModalService, private router: Router) { }

  ngOnInit() {
    let _this = this;
    if(cookie.getCookie("isLogin")){ //用户已登录
      _this.router.navigateByUrl("/back/dashboard");
    }
  }

  // 提交表单
  submitForm(){
    let _this = this;
    getData_ajax.getFunc(config.baseUrl+'/loginService/GetloginData','post',_this.user_login_info,function(data){
        if(data.isError){
          _this.modalService.error({
            nzTitle: '登录失败',
            nzContent: '用户名或密码错误，请重新输入...'
          });
        }else if(!data.isError){
          cookie.setCookie("isLogin",true,8000); // 存储用户cookie登录信息
          // localStorage.setItem("isLogin", 'true');
          _this.modalService.success({
            nzTitle: '登录成功',
            nzContent: '登录成功，正在跳转，请稍后...'
          });
          setTimeout(()=>{
            _this.router.navigateByUrl("/back/dashboard");
          }, 2000)
        }
      },
      function(error){
        console.log(error)
      })
  }

}

